package com.qf.rbac2202.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.rbac2202.annotation.DataFilter;
import com.qf.rbac2202.sys.entity.SysDept;
import com.qf.rbac2202.sys.entity.SysRole;
import com.qf.rbac2202.sys.entity.SysUser;
import com.qf.rbac2202.sys.mapper.SysUserMapper;
import com.qf.rbac2202.sys.service.ISysDeptService;
import com.qf.rbac2202.sys.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.rbac2202.utils.Constants;
import com.qf.rbac2202.utils.PageQueryUtils;
import com.qf.rbac2202.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author dong_
 * @since 2022-10-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
	@Autowired
	ISysDeptService iSysDeptService;

	@DataFilter
	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PageVo getList( Map< String, Object > params ) {
		IPage< SysUser > page1=PageQueryUtils.getPage( params );

		String username=(String) params.get( "username" );

		//
		String sql=(String) params.get( Constants.DATA_FILETER );
		//判断传过来的角色名是否为空
		QueryWrapper< SysUser > sysRoleQueryWrapper=new QueryWrapper<>( );
		sysRoleQueryWrapper
				.eq( StringUtils.isNotBlank( username ),"username",username )
				.eq( "status",1 )
				.apply(StringUtils.isNotBlank( sql ) ,sql );


		this.page( page1,sysRoleQueryWrapper );

		//获取所有的对象
		List< SysUser > records=page1.getRecords( );

		//查询所有的部门对象
		List< SysDept > list=iSysDeptService.list( );

		HashMap< Long, String > hashMap=new HashMap<>( );

		//将流转化为map集合
		list.stream().forEach( s->hashMap.put( s.getDeptId(),s.getName() ) );

		records.forEach(s -> s.setDeptName(  hashMap.get(  s.getDeptId() )  ) );

		PageVo pageVo=new PageVo( page1 );
		return pageVo;
	}
}
